//
//  importutils.mm
//  BioBreak
//
//  Created by terry franguiadakis on 3/24/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "importutils.h"
#import "Texture2D.h"
#import "EAGLView.h"
#import "bbmath.h"
#include "texture.h"

bool SystemTextureLoad(const char* i_filename, unsigned int& o_texid, unsigned int& o_width, unsigned int& o_height, float& o_maxS, float& o_maxT)
{
    NSString* filename = [[NSString alloc] initWithCString:i_filename];
    // retain filename?
    
    Texture2D* texture = [[EAGLView sharedSingleton] GetTexture:filename];
    
    if (texture != nil)
    {
        o_texid = [texture name];
        o_width = [texture pixelsWide];
        o_height= [texture pixelsHigh];
        o_maxS  = [texture maxS];
        o_maxT  = [texture maxT];
        return true;
    }
    
	// Texture dimensions must be a power of 2. If you write an application that allows users to supply an image,
	// you'll want to add code that checks the dimensions and takes appropriate action if they are not a power of 2.
    UIImage* uiImage = [UIImage imageNamed:filename];
    
    if(uiImage != nil)
    {
        Texture2D* newTexture = [[EAGLView sharedSingleton] CreateTexture:filename];
        
        if (newTexture == nil)
        {
            // something is horribly wrong...
#if DEBUG
            NSLog(@"Failed to create texture2d for image %s- something is horribly wrong.\n", i_filename);
#endif
            return false;
        }
        [newTexture initWithImage:uiImage];
        o_texid = [newTexture name];
        o_width = [newTexture pixelsWide];
        o_height= [newTexture pixelsHigh];
        o_maxS  = [newTexture maxS];
        o_maxT  = [newTexture maxT];
        
#if DEBUG
        if (!IsPowerof2(o_width) || !IsPowerof2(o_height))
        {
            NSLog(@"image %s is does not have power of two dimensions (%d %d)\n", o_width, o_height);
        }
#endif
        // Set the texture parameters to use a minifying filter and a linear filer (weighted average)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        
        return true;
    }
    
    // $$$ (TEF) - release imageRef? 
    NSLog(@"Failed to load Texturemap! %s", i_filename);
    return false;
}

void SystemTextureDelete(unsigned int i_texid)
{
    [[EAGLView sharedSingleton] DeleteTexture:i_texid];
}

bool SystemCreateLabel(CLabel* o_label, const char* i_characterSet, const char* i_fontName, int i_fontSize)
{
    NSString* charSet = [[NSString alloc] initWithCString:i_characterSet];
    NSString* fontname= [[NSString alloc] initWithCString:i_fontName];
    
    Texture2D* newTexture = [[EAGLView sharedSingleton] CreateTexture:charSet];
    if (newTexture != nil)
    {
        [newTexture initWithString:charSet fontName:fontname fontSize:i_fontSize];
        unsigned int width = [newTexture pixelsWide];
        unsigned int height= [newTexture pixelsHigh];
        std::string sscharset = i_characterSet;
        o_label->Init([newTexture name], 
                     width,
                     height,
                     Vector2f([newTexture maxS], [newTexture maxT]),
                     i_fontSize);
    }
    
//    UIFont* font = [UIFont fontWithName:fontname size:i_fontSize];
//    NSLog(@"font - asc%f desc%f lead%f cap%f xh%f ps%f\n", [font ascender], [font descender], [font leading], [font capHeight], [font xHeight], [font pointSize]);
//    NSLog(@"%@\n", [newTexture description]);
    
    return false;
}

// EOF

